home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT42.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  9.0 KB  |  328 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 42                         
  5.                                           
  6.  Demonstrates the polygon routines available.  It rotates a triangle         
  7.  in the middle of the screen using either a hollow, solid or Gouraud shaded  
  8.  polygon.  Also introduced is the dirty rectangle method of updating the     
  9.  screen.                                                                     
  10.                                           
  11.  Once the first three polygon methods are shown, a textured mapped polygon   
  12.  is shown.                                                                   
  13.                                           
  14.  *** PROJECT ***                                                             
  15.  This program requires the file WGT5_WC.LIB to be linked.                    
  16.                                           
  17.  *** DATA FILES ***                                                          
  18.  TEXTMAP.CEL must be in your executable directory.
  19.                                WATCOM C++ VERSION 
  20. ==============================================================================
  21. */
  22.  
  23. #include <math.h>
  24. #include <wgt5.h>
  25.  
  26. short num_sides;           /* Number of sides for the current polygon.
  27.                 For the first loop this is 3, and the second 
  28.                 loop it is 4. */
  29.  
  30. short polytype;            /* Current polygon type to draw */
  31. short currot;              /* Current rotation of the triangle */
  32.  
  33. short isin[360];           /* Sin values 0-360,  multiplied by 1024 */
  34. short icos[360];           /* Cos values 0-360,  multiplied by 1024 */
  35.  
  36. typedef struct {         /* Dirty rectangle structure */
  37.   short x1;
  38.   short y1;
  39.   short x2;
  40.   short y2;              /* Keeps track of which area on the screen */
  41. } rect;                  /* has been changed */
  42. rect lastrect;           /* Dirty rectangle for last frame */
  43. rect thisrect;           /* Dirty rectangle for current frame */
  44.  
  45. tpolypoint plainpoly[4]; /* Holds polygon points before rotation */
  46. tpolypoint rotatedpoly[4];  /* After rotation */
  47.  
  48. color pal[256];
  49. short oldmode;
  50. block other;             /* A background page for dirty rectangles */
  51. block texturemap;        /* A texture bitmap (128x128) */
  52. short square_size;         /* Size of the texture mapped square */
  53. short size_dir;            /* 1 or -1, added to square_size */
  54.  
  55.  
  56. void clear_last(void)
  57. {
  58.   /* Do clipping for smallest area update */
  59.   if (lastrect.x1 < 0)
  60.     lastrect.x1 = 0;
  61.   if (lastrect.x2 > 319)
  62.     lastrect.x2 = 319;
  63.   if (lastrect.y1 < 0)
  64.     lastrect.y1 = 0;
  65.   if (lastrect.y2 > 199)
  66.     lastrect.y2 = 199;
  67.   wsetcolor (0);
  68.   wbar (lastrect.x1, lastrect.y1, lastrect.x2, lastrect.y2);
  69.   /* Clear out the area that was drawn in last frame */
  70. }
  71.  
  72.  
  73. void update_screen(void)
  74. {
  75.   if (lastrect.x1 > thisrect.x1)
  76.     lastrect.x1 = thisrect.x1;
  77.   if (lastrect.x2 < thisrect.x2)
  78.     lastrect.x2 = thisrect.x2;
  79.   if (lastrect.y1 > thisrect.y1)
  80.     lastrect.y1 = thisrect.y1;
  81.   if (lastrect.y2 < thisrect.y2)
  82.     lastrect.y2 = thisrect.y2;
  83.   /* See if the previous frame was larger in any direction. If it is, enlarge
  84.      the area so it will copy black over the previous frame. */
  85.  
  86.   /* Do clipping */
  87.   if (lastrect.x1 < 0)
  88.     lastrect.x1 = 0;
  89.   if (lastrect.x2 > 319)
  90.     lastrect.x2 = 319;
  91.   if (lastrect.y1 < 0)
  92.     lastrect.y1 = 0;
  93.   if (lastrect.y2 > 199)
  94.     lastrect.y2 = 199;
  95.  
  96.   wcopyscreen(lastrect.x1, lastrect.y1, lastrect.x2, lastrect.y2, other,
  97.           lastrect.x1, lastrect.y1, NULL);
  98.   /* Copy from our second page to the visual page. */
  99.  
  100.   lastrect.x1 = thisrect.x1;
  101.   lastrect.y1 = thisrect.y1;
  102.   lastrect.x2 = thisrect.x2;
  103.   lastrect.y2 = thisrect.y2;
  104.   /* Make the last rectangle = current rectangle */
  105. }
  106.  
  107.  
  108. void makesin_cos_tables (void)
  109. /* Calculates the tables for our 2D rotation */
  110. {
  111.   short i;
  112.  
  113.   for (i = 0; i < 360; i++)
  114.   {
  115.     isin[i] = sin(3.1415 * ((double)i / 180.0)) * 1024;
  116.     icos[i] = cos(3.1415 * ((double)i / 180.0)) * 1024;
  117.   }
  118. }
  119.  
  120.  
  121. void rotatepoly (short cnx, short cny, tpolypoint *plain, tpolypoint *rotated, 
  122.          short numpoints, short rotation)
  123. /* Rotates a set of polygon points, given the center of the polygon, number
  124.    of points, and the rotation in degrees. The points to be rotated are in 
  125.    "plain", and the new points are stored in "rotated". */
  126. {
  127.   short i;
  128.   short x, y;
  129.   short x2, y2;
  130.  
  131.   for (i = 0; i < numpoints; i++)
  132.   {
  133.     x = plain[i].x - cnx;
  134.     y = plain[i].y - cny;
  135.  
  136.     x2 = ((int)x * icos[rotation] - (int)y * isin[rotation]) >> 10;
  137.     y2 = ((int)x * isin[rotation] + (int)y * icos[rotation]) >> 10;
  138.     rotated[i].x = x2 + cnx;
  139.     rotated[i].y = y2 + cny;
  140.     rotated[i].sx = plain[i].sx;
  141.     rotated[i].sy = plain[i].sy;
  142.   }
  143. }
  144.  
  145.  
  146. void make_triangle (void)
  147. /* Set up some points to make a triangle */
  148. {
  149.   plainpoly[0].x = 160;
  150.   plainpoly[0].y = 60;
  151.   plainpoly[0].sx = 5;
  152.  
  153.   plainpoly[1].x = 120;
  154.   plainpoly[1].y = 140;
  155.   plainpoly[1].sx = 50;
  156.  
  157.   plainpoly[2].x = 200;
  158.   plainpoly[2].y = 140;
  159.   plainpoly[2].sx = 100;
  160. }
  161.  
  162.  
  163. void make_square (void)
  164. /* Set up some points to make a square */
  165. {
  166.   plainpoly[0].x = 160 - square_size;
  167.   plainpoly[0].y = 100 - square_size;
  168.   plainpoly[0].sx = 0;   /* This sets the offset into the texture bitmap */
  169.   plainpoly[0].sy = 0;
  170.  
  171.   plainpoly[1].x = 160 + square_size;
  172.   plainpoly[1].y = 100 - square_size;
  173.   plainpoly[1].sx = 127;
  174.   plainpoly[1].sy = 0;
  175.  
  176.   plainpoly[2].x = 160 + square_size;
  177.   plainpoly[2].y = 100 + square_size;
  178.   plainpoly[2].sx = 127;
  179.   plainpoly[2].sy = 127;
  180.  
  181.   plainpoly[3].x = 160 - square_size;
  182.   plainpoly[3].y = 100 + square_size;
  183.   plainpoly[3].sx = 0;
  184.   plainpoly[3].sy = 127;
  185. }
  186.  
  187.  
  188. void make_palette (void)
  189. /* Make a shaded palette for Gouraud shading */
  190. {
  191.   short i;
  192.  
  193.   for (i = 0; i < 64; i++)
  194.   {
  195.     wsetrgb (i, 0, i / 2, i, pal);
  196.     wsetrgb (i + 64, i, (63 - i) / 2, (63 - i), pal);
  197.   }
  198.   wsetrgb (1, 63, 63, 63, pal);
  199.   wsetpalette (0, 255, pal);
  200. }
  201.  
  202.  
  203. void find_edges (void)
  204. /* Scans through all points in the polygon, and sees if the dirty rectangle
  205.    area should be increased to include the point. */
  206. {
  207.   short i;
  208.  
  209.   for (i = 0; i < num_sides; i++)
  210.   {
  211.     if (rotatedpoly[i].x < thisrect.x1)
  212.       thisrect.x1 = rotatedpoly[i].x;
  213.     if (rotatedpoly[i].x > thisrect.x2)
  214.       thisrect.x2 = rotatedpoly[i].x;
  215.     if (rotatedpoly[i].y < thisrect.y1)
  216.       thisrect.y1 = rotatedpoly[i].y;
  217.     if (rotatedpoly[i].y > thisrect.y2)
  218.       thisrect.y2 = rotatedpoly[i].y;
  219.     /* See if the polygon is larger than the current area to update.
  220.        If it is, enlarge the area so all polygons fit inside. */
  221.   }
  222. }
  223.  
  224.  
  225. void main(void)
  226. {
  227.   oldmode = wgetmode ();
  228.  
  229.   printf ("WGT Example #42\n\n");
  230.   printf ("Polygons and texture-mapping are demonstrated using some optimized dirty-\n");
  231.   printf ("rectangle code. Press a key to advance to the next type of polygon.\n");
  232.   printf ("\n\nPress any key to continue.\n");
  233.   getch ();
  234.  
  235.   vga256();
  236.   other = wnewblock (0, 0, 319, 199);
  237.   winitpoly (WGT_SYS.yres);
  238.   makesin_cos_tables ();
  239.   make_palette ();
  240.   make_triangle ();
  241.  
  242.   wsetscreen (other);
  243.   lastrect.x1 = 0; 
  244.   lastrect.y1 = 0; 
  245.   lastrect.x2 = 319; 
  246.   lastrect.y2 = 199;
  247.   currot = 0;
  248.   num_sides = 3;
  249.  
  250.   do {
  251.     thisrect.x1 = 319; 
  252.     thisrect.y1 = 199; 
  253.     thisrect.x2 = 0; 
  254.     thisrect.y2 = 0;
  255.     clear_last ();
  256.     rotatepoly (160, 100, plainpoly, rotatedpoly, num_sides, currot);
  257.  
  258.     find_edges ();
  259.  
  260.     wsetcolor (60);  
  261.     switch (polytype)
  262.     {
  263.       case 0: whollowpoly (rotatedpoly, num_sides, 0, 0, CLOSED_POLY);
  264.           break;
  265.       case 1: wsolidpoly (rotatedpoly, num_sides, 0, 0, NULL);
  266.           break;
  267.       case 2: wgouraudpoly (rotatedpoly, num_sides, 0, 0);
  268.           break;
  269.     } 
  270.     update_screen ();
  271.  
  272.     currot++;             /* Rotate the polygon */
  273.     if (currot > 359)
  274.       currot -= 360;
  275.  
  276.     if (kbhit ())
  277.     {
  278.       getch ();
  279.       polytype++;         /* Go to the next type of polygon */
  280.     }
  281.   } while (polytype < 3);
  282.  
  283.   /* Now do a texture mapped polygon */
  284.   num_sides = 4;
  285.   make_square ();
  286.   wcls (0);
  287.   lastrect.x1 = 0; 
  288.   lastrect.y1 = 0; 
  289.   lastrect.x2 = 319; 
  290.   lastrect.y2 = 199;
  291.   currot = 0;
  292.   texturemap = wloadcel ("textmap.cel", pal);
  293.   wsetpalette (0, 255, pal);
  294.   square_size = 63;
  295.   size_dir = -1;
  296.  
  297.   do {
  298.     thisrect.x1 = 319; 
  299.     thisrect.y1 = 199; 
  300.     thisrect.x2 = 0; 
  301.     thisrect.y2 = 0;
  302.  
  303.     square_size += size_dir;
  304.     if (square_size < 30)         /* Make the polygon get bigger and smaller */
  305.       size_dir = 1;
  306.     if (square_size > 100)
  307.       size_dir = -1;
  308.     make_square ();
  309.  
  310.     clear_last ();
  311.     rotatepoly (160, 100, plainpoly, rotatedpoly, num_sides, currot);
  312.     find_edges ();
  313.  
  314.     wtexturedpoly (rotatedpoly, num_sides, 0, 0, texturemap, NORMAL);
  315.     update_screen ();
  316.  
  317.     currot += 3 * size_dir;             /* Rotate the polygon */
  318.     if (currot > 359)
  319.       currot -= 360;
  320.     if (currot < 0)
  321.       currot += 360;
  322.   } while (!kbhit ());
  323.   wdeinitpoly ();
  324.   wfreeblock (other);
  325.   wfreeblock (texturemap);
  326.   wsetmode (oldmode);
  327. }
  328.